import { Subscribe } from "../../../models/subscribe";
const subscribe = new Subscribe();
import {
  handlerGobackClick,
  onlyChangeRouter,
  hint,
  uploadFormdata,
} from "../../../utils/util";
const app = getApp();
import User from "../../../models/user";
const user = new User();

Page({
  data: {
    baseImgUrl: "",
    activeTab: 0,
    currentHousing: {},

    isVisitor: true,
    commid: "",

    scrollH: "auto",
    tabOne0: [],
    tabOne1: [],

    //加载中状态
    loading: false,
    loading1: false,
    //没有更多了
    noMore: false,
    noMore1: false,
    //加载失败
    loadingFailed: false,
    loadingFailed1: false,
    //当前已加载页数
    pageNum: 1,
    pageNo1: 1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      currentHousing: options.houseId
        ? options
        : wx.getStorageSync("mydwellOption"),
    });

    if (options.houseId) {
      wx.setStorageSync("mydwellOption", options);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var query = wx.createSelectorQuery();
    query
      .select("#scrollH")
      .boundingClientRect((res) => {
        let height = res.height - 202;
        // let height = res.height - 44;
        this.setData({
          scrollH: height,
        });
      })
      .exec();
  },
  handlerGobackClick,
  onlyChangeRouter,
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
    });

    this.initPageData();
  },
  scrollBottom() {
    if (!this.data.loading && !this.data.noMore) {
      this.setData({
        loading: true,
        pageNum: this.data.pageNum + 1,
      });
      this.getData(
        "addressDetails",
        "loading",
        "tabOne0",
        "pageNum",
        "loadingFailed",
        "noMore",
        true,
        this.data.currentHousing.houseId
      );
    }
  },
  scrollBottom1() {
    if (!this.data.loading1 && !this.data.noMore1) {
      if (!this.data.loading1 && !this.data.noMore1) {
        this.setData({
          loading1: true,
          pageNo1: this.data.pageNo1 + 1,
        });
        this.getData(
          "addressDetails",
          "loading1",
          "tabOne1",
          "pageNo1",
          "loadingFailed1",
          "noMore1",
          true,
          this.data.currentHousing.houseId
        );
      }
    }
  },
  onChangetab(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  myAddDetail(e) {
    let { item } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/owner-app/pages/my-dwell-detailsthei/index?memberId=${item.memberId}&houseId=${this.data.currentHousing.houseId}`,
    });
  },
  catchTap() {},
  anewHandle(e) {
    let data = e.target.dataset.item;
    let params = {
      id: data.houseMemberId,
      passSign: 0,
    };
    uploadFormdata({
      url: "/threefact/baseMember/audit",
      method: "POST",
      data: params,
    })
      .then((res) => {
        if (res.data.code == 1000) {
          hint("提交审核成功");
          setTimeout(() => {
            this.initPageData();
          }, 500);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
  myDelete(e) {
    let { houseid } = e.currentTarget.dataset;
    user.deleteHouseHold(houseid).then((res) => {
      if (res.code == 1000) {
        hint("删除成功");
        this.initPageData();
      }
    });
  },
  /**
   *
   * @param methodName   同类下面的方法名称
   * @param loadingName  加载状态变量
   * @param dataName     data中设定的数据
   * @param isPage       是否是分页查询
   * @param houseId       房间id
   */
  getData(
    methodName,
    loadingName,
    dataName,
    pageNoName,
    loadingFailedName,
    noMoreName,
    isPage,
    houseId
  ) {
    user[methodName](houseId, this.data[pageNoName], 10)
      .then((res) => {
        this.setData({
          [loadingName]: false,
        });

        var pagesSecond = 0;
        var sizeSecond = 0;
        var totalSecond = 0;
        var dataList = [];
        if (dataName == "tabOne1") {
          pagesSecond = res.result.IAdded.current;
          sizeSecond = res.result.IAdded.size;
          totalSecond = res.result.IAdded.total;
          dataList = res.result.IAdded.records;
        } else {
          pagesSecond = res.result.addressDetails.current;
          sizeSecond = res.result.addressDetails.size;
          totalSecond = res.result.addressDetails.total;
          dataList = res.result.addressDetails.records;
        }

        //有分页
        if (isPage) {
          this.setData({
            [dataName]: this.data[dataName].concat(dataList),
          });
        }
        //没有分页就直接赋值
        else {
          this.setData({
            [dataName]: dataList,
          });
        }

        if (pagesSecond * sizeSecond >= totalSecond) {
          this.setData({
            [noMoreName]: true,
          });
          return false;
        }
      })
      .catch((err) => {
        this.setData({
          [loadingFailedName]: true,
        });
      });
  },
  initPageData() {
    user
      .addressDetails(this.data.currentHousing.houseId)
      .then((res) => {
        if (res.code == 1000) {
          this.setData({
            tabOne0: res.result.addressDetails.records,
            tabOne1: res.result.IAdded.records,
          });

          //居住地址--添加住户的commid
          wx.setStorageSync("formresidentCommId", {
            commId: res.result.addressDetails.records[0].commId,
            currAdd: this.data.currentHousing.addressDesc,
            houseId: this.data.currentHousing.houseId,
          });
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
